package com.hmall.trade.listener;


import com.hmall.trade.domain.po.Order;
import com.hmall.trade.service.IOrderService;
import lombok.RequiredArgsConstructor;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

// Mq消息的接收方
@Component
@RequiredArgsConstructor
public class PayStatusListener {

    private final IOrderService orderService;
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "trade.pay.success.queue", durable = "true"),
            exchange = @Exchange(name = "trade.exchange"),
            key = "pay.success"
    ))
    public void payStatus(Long id) {
        // 获取订单
        Order byId = orderService.getById(id);
        // 为空或者订单状态不是待支付的不可更改为已支付
        if (byId == null || byId.getStatus() != 1) {
            return;
        }
        orderService.markOrderPaySuccess(id);
    }
}
